package com.example.qihuibao.module.service

import android.app.Service
import android.content.Intent
import android.os.Binder
import android.os.IBinder

/**
 * 定时任务
 */
class TimerService : Service() {
    private var callback:CallBack? = null
    private var threadRunning = true

    override fun onCreate() {
        super.onCreate()
        Thread {
            while (threadRunning) {
                this.callback?.scheduledTasks()
                Thread.sleep(1000)
            }
        }.start()
    }

    override fun onBind(intent: Intent?): IBinder? {
        return MyBinder()
    }

    override fun onDestroy() {
        super.onDestroy()
        threadRunning = false
    }

    fun setCallback(callBack: CallBack) {
        this.callback = callBack
    }

    inner class MyBinder : Binder() {
        fun getService(): TimerService {
            return this@TimerService
        }
    }

    interface CallBack {
        fun scheduledTasks()
    }
}